package com.wanglin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglin.reggie.common.CustomException;
import com.wanglin.reggie.dto.DishDto;
import com.wanglin.reggie.entity.Dish;
import com.wanglin.reggie.entity.DishFlavor;
import com.wanglin.reggie.mapper.DishMapper;
import com.wanglin.reggie.service.DishFlavorService;
import com.wanglin.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * DishServiceImpl
 *
 * @author 王林
 * @version 1.0
 * @description 菜品服务实现类
 * @date 2025/5/29 03:23
 * @projectName reggie_take_out
 * @package com.wanglin.reggie.service.impl
 * @className com.wanglin.reggie.service.impl.DishServiceImpl
 * @copyright Copyright 2020 Theofanis, Wang Lin All rights reserved.
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品信息
        this.save(dishDto);
        // 获取菜品id
        Long id = dishDto.getId();
        // 获取菜品口味列表
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 遍历口味列表，设置菜品id
        flavors = flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        // 批量保存菜品口味信息
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    // 根据id获取菜品信息，并包含口味信息
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        List<DishFlavor> flavors = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>()
                .eq(DishFlavor::getDishId, dish.getId()));
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    // 声明一个事务
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 修改菜品信息
        this.updateById(dishDto);
        // 删除菜品口味信息
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>()
                .eq(DishFlavor::getDishId, dishDto.getId()));
        // 保存菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        // 更改菜品售卖状态（可批量）
        Dish dish = new Dish();
        dish.setStatus(status);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.setEntity(dish);
        this.update(queryWrapper);
    }

    /**
     *套餐批量删除和单个删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //构造条件查询器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //先查询该菜品是否在售卖，如果是则抛出业务异常
        queryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);
        for (Dish dish : list) {
            Integer status = dish.getStatus();
            //如果不是在售卖,则可以删除
            if (status == 0){
                this.removeById(dish.getId());
            }else {
                //此时应该回滚,因为可能前面的删除了，但是后面的是正在售卖
                throw new CustomException("删除菜品中有正在售卖菜品,无法全部删除");
            }
        }

    }
}
